<%@ page pageEncoding="utf-8"%>
<%@page import="org.myhibernate.util.Json"%>
<%@page import="org.myhibernate.Template"%>
<%@page import="org.myhibernate.ProxyTemplate"%>
<%@page import="org.json.JSONObject"%>
<%@page import="org.personal.entity.Page"%>
<%@page import="org.personal.entity.Layout"%>
<%@page import="org.personal.entity.User"%>
<%@page import="org.personal.Constants"%>
<%@page import="org.personal.entity.UserPage"%>
<%@page import="org.personal.util.UserUtil"%>
<%@page import="org.personal.entity.Portlet"%>
<%@page import="java.util.List"%>
<%@page import="org.myhibernate.core.method.PageSizeQuery"%>
<%
String portletId=request.getParameter("id");
String pageId=request.getParameter("pageId");
Json json=new Json();
User user=(User)request.getSession().getAttribute(Constants.USER);
UserPage userPage=null;
List portletList=null;

	Template template_portlet=new ProxyTemplate(Portlet.class).getInstance();
	Template template_page=new ProxyTemplate(Page.class).getInstance();
	Template template_userPage=new ProxyTemplate(UserPage.class).getInstance();
	try
	{
		if(user != null)
		{
			Portlet portlet=(Portlet)template_portlet.get(Integer.parseInt(portletId));
			PageSizeQuery queryUserPage=template_userPage.getPageSizeQuery();
			int count=queryUserPage.query(" where userId='"+user.getUid()+"' and pageId='"+pageId+"' ");
			if(count>0)
			{
				userPage=(UserPage)queryUserPage.getListObject(0,1).get(0);
			}
			if(portlet != null)
			{	
				UserUtil userUtil=new UserUtil();
				List editUserList=user.getGroups();
				editUserList.add(Constants.USER_ALLUSER);
				editUserList.add(user.getUid());
				boolean editable=userUtil.hasCommon(portlet.getEditUsers(),editUserList);
				if(editable)
				{
					boolean isCreateUserPage=false;
					if(userPage != null)
					{
					 	portletList=userPage.getPortlets();
						String portletInfo="";
						for(int i=0;i<portletList.size();i++)
						{
							portletInfo=portletList.get(i)+"";
							if(portletInfo.endsWith(":"+portletId))
							{
								portletList.remove(i);
								break;
							}
						}
					}else
					{
						isCreateUserPage=true;
						Page pageObject=(Page)template_page.get(Integer.parseInt(pageId));
						
						userPage=new UserPage();
						userPage.setUserId(user.getUid());
						userPage.setPageId(Integer.parseInt(pageId));
						userPage.setLayoutId(pageObject.getLayout());
						portletList=pageObject.getPortlets();
						String portletInfo="";
						for(int i=0;i<portletList.size();i++)
						{
							portletInfo=portletList.get(i)+"";
							if(!portletInfo.endsWith(":"+portletId))
							{
								userPage.getPortlets().add(portletInfo);
							}
						}
					}
					
					if(isCreateUserPage)
					{	
						template_userPage.save(userPage);
					}else
					{
						template_userPage.update(userPage);
					}
					json.add("result","ok");
				}else
				{
					
					json.add("result","no");
					json.add("message","您没有移除权限");
				}
			}else
			{
				//portlet已经不存在的情况下，清除我的userpage中记录
				if(userPage != null)
				{
					portletList=userPage.getPortlets();
					String portletInfo="";
					for(int i=0;i<portletList.size();i++)
					{
						portletInfo=portletList.get(i)+"";
						if(portletInfo.endsWith(":"+portletId))
						{
							portletList.remove(i);
							break;
						}
					}
					template_userPage.update(userPage);	
				}else
				{
						Page pageObject=(Page)template_page.get(Integer.parseInt(pageId));
						
						userPage=new UserPage();
						userPage.setUserId(user.getUid());
						userPage.setPageId(Integer.parseInt(pageId));
						userPage.setLayoutId(pageObject.getLayout());
						portletList=pageObject.getPortlets();
						String portletInfo="";
						for(int i=0;i<portletList.size();i++)
						{
							portletInfo=portletList.get(i)+"";
							if(!portletInfo.endsWith(":"+portletId))
							{
								userPage.getPortlets().add(portletInfo);
							}
						}
						template_userPage.save(userPage);
				}
				json.add("result","ok");
				json.add("message","未找到");
			}
		}else
		{
			json.add("result","no");
			json.add("message","未登录");
		}//endif user!=null
		
	}catch(Exception exception)
	{
		json.add("result","no");
		json.add("message",exception.toString().trim());
	}
	finally
	{
		template_portlet.close();
		template_page.close();
		template_userPage.close();
	}
out.println(json);
%>
